package com.weiwei.netphone;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.Contacts.Data;
import android.provider.ContactsContract.RawContacts;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.hwtx.jytxdh.R;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.sso.UMSsoHandler;
import com.weiwei.base.application.VsApplication;
import com.weiwei.base.common.CustomLog;
import com.weiwei.base.common.VsUtil;
import com.weiwei.base.dataprovider.GlobalVariables;
import com.weiwei.base.dataprovider.VsUserConfig;
import com.weiwei.base.db.provider.VsPhoneCallHistory;
import com.weiwei.base.fragment.FragmentIndicator;
import com.weiwei.base.fragment.FragmentIndicator.OnIndicateListener;
import com.weiwei.base.fragment.VsDialFragment;
import com.weiwei.base.widgets.CustomDialog2;
import com.weiwei.base.widgets.TitleBarView;
import com.zte.functions.ad.AdManager;

/**
 * @author yangyu 功能描述：主Activity类，继承自FragmentActivity
 */
public class VsMainActivity extends FragmentActivity implements OnClickListener {
    private static final String TAG = VsMainActivity.class.getSimpleName();
    public Fragment[] mFragments;
    public Context mContext;
    //private TextView mAddContactTV;
    private int flag = -1;
    /**
     * 打电话按钮
     */
    private RelativeLayout ll_mybottom;
    /**
     * 呼叫按钮
     */
    public Button mCallBtn;

    /**
     * 拨号按钮是否显示
     */
    public static boolean isShow = true;
    /**
     * 需要跳转的fragment
     */
    public int indicator = 0;
    /**
     * 第一个消息
     */
    public static String msg1 = "";
    /**
     * 是否为新注册，true为新注册、false不是
     */
    public static String firstreg = "";
    /**
     * 是否绑定手机，true为绑定，false未绑定
     */
    public static String check = "";
    /**
     * 第一次绑定，true为第一次，false不是
     */
    public static String firstbind = "";
    /**
     * 底部导航栏
     */
    private FragmentIndicator mIndicator = null;
    /**
     * 关闭键盘
     */
    private LinearLayout vs_mian_keyborad_layout;
    /**
     * 联系人
     */
    private LinearLayout vs_mian_contact_layout;

    /**
     * 更新tab选中图标
     */
    private final char MSG_ID_UPDATE_TAB_FOUSE = 150;
    /**
     * 更新未选中
     */
    private final char MSG_ID_UPDATE_TAB_NOMAL = 151;
    /**
     * 显示余额相关提示
     */
    private final char MSG_ID_SHOW_BALANCE = 152;
    // 更新消息提醒
    private final char MSG_ID_MESSAGE_SHOW = 158;
    //标题
    private com.weiwei.base.widgets.TitleBarView mContactsTitleBarView;
    private LinearLayout titleMain;
   
    /**
     * 标题
     */
    private TextView titleView;
    private ImageView tv_left;
    private ImageView tv_right;
    private EditText btn;
    private String phoneNumberString = ""; 
    private String save_name = "";
    private VsDialFragment mFragment;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            switch (msg.what) {
                case MSG_ID_MESSAGE_SHOW:// 更新消息状态
                    Bundle b = msg.getData();
                    // b.getString("countString");
                    FragmentIndicator.showHideRed(b.getInt("countString"));
//                    if(indicator==0){
//                    	 mContactsTitleBarView.setTextMms(b.getInt("countString"),false);
//                    }else{
//                    	 mContactsTitleBarView.setTextMms(b.getInt("countString"),true); //更新信息                       	
//                    }                                 
                    break;
                case MSG_ID_UPDATE_TAB_FOUSE:
                    mIndicator.setTabImageFouse(msg.getData().getBoolean("isopen", false));
                    if (flag == 0) {// 自己点击自己有动画CollinWang2015.03.25
                		mIndicator.setTabImageFouse2(msg.getData().getBoolean("isopen", false));
                	} else {
                		mIndicator.setTabImageFouse(msg.getData().getBoolean("isopen", false));
                	}
                    break;
                case MSG_ID_UPDATE_TAB_NOMAL:
                    mIndicator.setTabImage(msg.getData().getBoolean("isopen", false));
                    break;
                case MSG_ID_SHOW_BALANCE:
                    showBalance();
                    break;
            }
        }
    };
    private BroadcastReceiver mListenAdReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            CustomLog.i(TAG, "mListenAdReceiver(),....");

            if (AdManager.ACTION_UPDATE_AD.equals(action)){
                for (Fragment f : mFragments){
                    if (f instanceof VsDialFragment){
                        //((VsDialFragment)f).updateAd();
                    } 
                }
            }
        }
    };
    
    private void registerReceiver() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(AdManager.ACTION_UPDATE_AD);
        registerReceiver(mListenAdReceiver, filter);
    }

    private void unregisterReceiver() {
        unregisterReceiver(mListenAdReceiver);
    }
    /**
     * 单聊、群聊选择Dialog
     */
    private Dialog mDialogSelect;
    
    private void initAd() {
        AdManager.getInstance().initAd(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CustomLog.i(TAG, "MainFragment------onCreate(),...");
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);// 竖屏
        mContext = this;       
        setContentView(R.layout.vs_activity_main);    
        VsUtil.savedToSDCard("登录的uid=" + VsUserConfig.getDataString(mContext, VsUserConfig.JKey_KcId), "帐号信息");
        VsUtil.savedToSDCard("登录的手机号=" + VsUserConfig.getDataString(mContext, VsUserConfig.JKey_PhoneNumber), "帐号信息");
        VsUtil.savedToSDCard("========================================================", "帐号信息");
        inittitlebar();
        initView();
        initAd();

        Intent intent = getIntent();
        indicator = intent.getIntExtra("indicator", 0);

        if (intent.hasExtra("extra_conversation")) {
            indicator = 2;
            //findViewById(R.id.).setVisibility(View.VISIBLE);
        }
        if ((intent.getData() != null) && (intent.getData().getScheme().equals("rong"))) {
            indicator = 2;
            //findViewById(R.id.custom).setVisibility(View.VISIBLE);
        }
        GlobalVariables.curIndicator = indicator;
        msg1 = intent.getStringExtra("msg1");
        firstreg = intent.getStringExtra("firstreg");
        check = intent.getStringExtra("check");
        firstbind = intent.getStringExtra("firstbind");
        CustomLog.i("beifen", "msg1====" + msg1 + "    firstreg====" + firstreg + "    check====" + check + "    firstbind====" + firstbind);
        if (msg1 == null) {
            msg1 = "";
        }
        if (firstreg == null) {
            firstreg = "";
        }
        if (check == null) {
            check = "";
        }
        if (firstbind == null) {
            firstbind = "";
        }
        setFragmentIndicator(indicator);
        // 注册显示拨号和关闭拨号广播
        IntentFilter open_close_filter = new IntentFilter();
        open_close_filter.addAction(GlobalVariables.action_open_call_btn);
        open_close_filter.addAction(GlobalVariables.action_close_call_btn);
        open_close_filter.addAction(GlobalVariables.action_is_double_btn);
        mContext.registerReceiver(openOrCloseCall, open_close_filter);
        registerReceiver();
        // curIndicator = 0;// 不然每次退出。下次进入还会是当时退出界面
        // 获取Token建立拨打连接
phoneNumberString = VsUserConfig.getDataString(mContext, VsUserConfig.JKEY_NUM_PEOPLE, "0");//getResources().getString(R.string.phone_person_mobile);
        
        if (!phoneNumberString.equals("0")) {
        new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
					getQueryData(getResources().getString(R.string.product_name), phoneNumberString);			
				
			}
		}).start();
        }
        
        //下载启动页图片
        downLoadSplashAd();

        //registerReceiver();
        // handler.sendEmptyMessage(MSG_ID_SHOW_BALANCE);
        mFragment = (VsDialFragment)mFragments[0];
        VsApplication.getInstance().addActivity(this);
    }
    
    public void downLoadSplashAd() {
        //启动页广告
        String dataString = VsUserConfig.getDataString(mContext, VsUserConfig.JKEY_AD_CONFIG_0001, "");
        if (dataString.length() > 0) {
            try {
                JSONArray array = new JSONArray(dataString);
                for (int i = 0; i < array.length(); i++) {
                    JSONObject object = (JSONObject) array.get(i);
                    String image_url = object.getString("image_url");
                    saveUrlImg(image_url);
                    CustomLog.d("download", "下载图片: " + dataString);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
    
    private void saveUrlImg(String image_url) {
        new GetImg().execute(image_url);
        CustomLog.d("download", "image_url: " + image_url);
    }
    
    /**
     * 将url转换成bitmap异步任务
     */
    private class GetImg extends AsyncTask<String, Void, Bitmap> {

        @Override
        protected Bitmap doInBackground(String... strings) {
            HttpURLConnection con = null;//访问网络
            InputStream is = null;
            Bitmap bitmap = null;
            try {
                URL url = new URL(strings[0]);
                con = (HttpURLConnection) url.openConnection();
                con.setConnectTimeout(5 * 1000);
                con.setReadTimeout(5 * 1000);
                /*http 响应吗
                 * 200：成功
                 * 404：未找到
                 * 500：发生错误
                 */
                if (con.getResponseCode() == 200) {
                    is = con.getInputStream();
                    bitmap = BitmapFactory.decodeStream(is);
                    return bitmap;
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (is != null) {
                        is.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (con != null) {
                    con.disconnect();
                }
            }
            return null;
        }

        // onPostExecute在UI线程中执行命令
        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            String path = Environment.getExternalStorageDirectory().toString() + "/fs_img/chache";
            File path1 = new File(path);
            if (!path1.exists()) {
                path1.mkdirs();
            }
            //TODO 暂时只考虑保存一张图片
            File file = new File(path1, "splash.jpg");
            String filePath = file.getAbsolutePath();
            saveFileByBitmap(bitmap, filePath);


        }
    }
    
    /**
     * 保存图片到本地
     *
     * @param bitmap
     * @param newImagePath
     */
    private void saveFileByBitmap(Bitmap bitmap, String newImagePath) {
        File file = new File(newImagePath);
        try {
            FileOutputStream out = new FileOutputStream(file);
            if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)) {
                out.flush();
                out.close();
                //保存图片路径
                VsUserConfig.setData(mContext, GlobalVariables.JKEY_AD_SPLASHIMAG_PATH, newImagePath);
                CustomLog.d("download", "保存路径:" + newImagePath);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String[]columns={ ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER
            , ContactsContract.CommonDataKinds.Phone.CONTACT_ID};
    
    private void getQueryData(String name,String number) {
        ContentResolver resolver=getContentResolver();
        Cursor cursor=resolver.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
        Boolean ishas = false;
        while(cursor.moveToNext()){
            int idIndex = cursor.getColumnIndex(columns[0]);
            int displayNameIndex = cursor.getColumnIndex(columns[1]);
            int id = cursor.getInt(idIndex);
            String disPlayName = cursor.getString(displayNameIndex);
   
           
            if (disPlayName != null&&disPlayName.equals(getResources().getString(R.string.product_name))) {
            		ishas = true;
                    SharedPreferences cus_number = getSharedPreferences("number", 0);
                     save_name = cus_number.getString("numberString","1");
                     
                    if (!save_name.equals(phoneNumberString)) {
                    	
                        String h_name = getResources().getString(R.string.product_name);  
                        //根据姓名求id  
                        Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");  
                        ContentResolver h_resolver = getContentResolver();  
                        Cursor h_cursor = h_resolver.query(uri, new String[]{Data._ID},"display_name=?", new String[]{h_name}, null);  
                        
                        if(h_cursor.moveToFirst()){  
                        	int h_id = h_cursor.getInt(0);  
                        	//根据id删除data中的相应数据  
                        	resolver.delete(uri, "display_name=?", new String[]{h_name});  
                        	uri = Uri.parse("content://com.android.contacts/data");  
                        	resolver.delete(uri, "raw_contact_id=?", new String[]{h_id+""});  
                        	ishas = false;
                        } 
                    } 
            	}  
       
        }
        if (!ishas) {      	
        	insert(name, number);
		}
        cursor.close();
    }
    
    public boolean insert(String given_name, String mobile_number) {  
        try {  
            ContentValues values = new ContentValues();  
            // 下面的操作会根据RawContacts表中已有的rawContactId使用情况自动生成新联系人的rawContactId  
            Uri rawContactUri = getContentResolver().insert(  RawContacts.CONTENT_URI, values);  
            long rawContactId = ContentUris.parseId(rawContactUri); 
            String[] number = mobile_number.split(",");
            // 向data表插入姓名数据  
            	 if (given_name != "") {  
            		 values.clear();  
            		 values.put(Data.RAW_CONTACT_ID, rawContactId);  
            		 values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);  
            		 values.put(StructuredName.GIVEN_NAME, given_name);  
            		 getContentResolver().insert(ContactsContract.Data.CONTENT_URI,  
                        values);  
            	 }  
            for (int i = 0; i < number.length; i++) {	
            // 向data表插入电话数据  
            	if (mobile_number != "") {  
            		values.clear();  
            		values.put(Data.RAW_CONTACT_ID, rawContactId);  
            		values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);  
            		values.put(Phone.NUMBER, number[i]);  
            		values.put(Phone.TYPE, Phone.TYPE_MOBILE);  
            		getContentResolver().insert(ContactsContract.Data.CONTENT_URI,  
                        values);  
            	}  
            }
            
            
            SharedPreferences cus_number = getSharedPreferences("number", 0);
            SharedPreferences.Editor editor = cus_number.edit();
            editor.putString("numberString",phoneNumberString);
            editor.commit();
        }  
  
        catch (Exception e) {  
            return false;  
        }  
        return true;  
    } 
    
    @Override
    protected void onNewIntent(Intent intent) {
        handleMissedCallNotify(intent);
        super.onNewIntent(intent);
    }

    
    private void handleMissedCallNotify(Intent intent) {
        boolean isMissCallNotify = intent.getBooleanExtra("is_miss_call_notification", false);
        CustomLog.i("VsMainActivity", "handleMissedCallNotify(), isMissCallNotify = " + isMissCallNotify);
        if (isMissCallNotify) {
            showFragment(0);
            mIndicator.setIndicator(0);
            Intent i = new Intent(VsUserConfig.JKey_CLOSE_USER_LEAD);
            // 拨号盘开启状态
            boolean isopen = VsUserConfig.getDataBoolean(mContext, VsUserConfig.JKEY_KEYBORD_IS_SHOW, true);
            if (isopen) {
                intent.putExtra("isopen", false);
                intent.putExtra("indicator", indicator);
            } else {
                intent.putExtra("isopen", true);
                intent.putExtra("indicator", indicator);
            }
            mContext.sendBroadcast(i);
            //findViewById(R.id.custom).setVisibility(View.GONE);
        }
    }

    private BroadcastReceiver openOrCloseCall = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = VsUtil.isNull(intent.getAction()) ? "" : intent.getAction();
            // 显示拨号按钮
            if (action.equals(GlobalVariables.action_open_call_btn)) {
                showCallAnimation();
                // 隐藏拨号按钮
            } else if (action.equals(GlobalVariables.action_close_call_btn)) {
                closeCallAnimation();
                // 是否显示两个按钮（直播回拨按钮）
            } else if (action.equals(GlobalVariables.action_is_double_btn)) {
                setisCall();
            }
        }
    };


    /**
     * 初始化组件
     */
    private void initView() {
    	titleView=(TextView) findViewById(R.id.sys_title_txt);
    	tv_left=(ImageView) findViewById(R.id.tv_left_contact);
    	tv_right=(ImageView) findViewById(R.id.tv_right_contact);
        vs_mian_keyborad_layout = (LinearLayout) findViewById(R.id.vs_mian_keyborad_layout);
        vs_mian_contact_layout = (LinearLayout) findViewById(R.id.vs_mian_contact_layout);
     
//         List<Fragment> lf = getSupportFragmentManager().getFragments();
//         for (Fragment fragment : lf) {
//			if (fragment instanceof VsDialFragment) {
//				mFragment = (VsDialFragment) fragment;
//				break;
//			}
//		}
        vs_mian_contact_layout.setOnLongClickListener(
				new View.OnLongClickListener() {

					@Override
					public boolean onLongClick(View v) {
//						btn.setText("");
//						intent_del.putExtra("del", "longdel");
//						mContext.sendBroadcast(intent_del);	
						mFragment.setTitleText();
						return false;
					}
				});
        ll_mybottom = (RelativeLayout) findViewById(R.id.ll_mybottom);
        mCallBtn = (Button) findViewById(R.id.btn_bottom_call);
        mIndicator = (FragmentIndicator) findViewById(R.id.indicator);

        // 拨号盘开启状态
        boolean isopen = VsUserConfig.getDataBoolean(mContext, VsUserConfig.JKEY_KEYBORD_IS_SHOW, true);
        // 初始化按钮状态
        mIndicator.setTabImageFouse(isopen);
        mIndicator.setTabText(isopen);
        // 设置监听事件
        vs_mian_keyborad_layout.setOnClickListener(this);
        vs_mian_contact_layout.setOnClickListener(this);
        mCallBtn.setOnClickListener(this);

        
       // mAddContactTV = (TextView) findViewById(R.id.tv_add_contact);
        //mAddContactTV.setOnClickListener(this);
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     */
    @Override
    public void onClick(View v) {
        
        switch (v.getId()) {
            case R.id.btn_bottom_call:
            	if (VsUtil.isFastDoubleClick()) {
                    return;
                }
                // 跳转打电话
                sendBroadcast(new Intent(GlobalVariables.action_dial_phone).setPackage(getPackageName()));
                break;
            case R.id.vs_mian_keyborad_layout:
            	if (VsUtil.isFastDoubleClick()) {
                    return;
                }
                Intent intent = new Intent(VsUserConfig.JKey_CLOSE_USER_LEAD);
                // 切换TAB改变清空编辑框数据
                mIndicator.setTabImageFouse(false);
                intent.putExtra("isopen", false);
                intent.putExtra("indicator", 0);
                mContext.sendBroadcast(intent);
                break;
            case R.id.vs_mian_contact_layout:
//                setFragmentIndicator(1);// 跳转到联系人
//                setTabImage(true);
//                closeCallAnimation();
            	mFragment.upTitleText();

        	
                
                
                break;
            default:
                break;
        }
    }

    /**
     * 显示拨打按钮动画
     */
    public void showCallAnimation() {
        if (!isShow) {
            ll_mybottom.setVisibility(View.VISIBLE);
            isShow = true;
            setisCall();
        }
    }

    /**
     * 设置是否是两个按钮
     */
    public void setisCall() {
        mCallBtn.setVisibility(View.VISIBLE);
    }

    /**
     * 隐藏拨打按钮动画
     */
    public void closeCallAnimation() {
        if (isShow) {
            isShow = false;
            ll_mybottom.setVisibility(View.GONE);
        }
    }

    /**
     * 设置tab导航栏按钮状态
     *
     * @param isopen
     */
    public void setTabImageFouse(boolean isopen) {
        Message msg = handler.obtainMessage();
        msg.what = MSG_ID_UPDATE_TAB_FOUSE;
        Bundle bundle = new Bundle();
        bundle.putBoolean("isopen", isopen);
        msg.setData(bundle);
        handler.sendMessage(msg);
    }

    /**
     * 更新未选中时的tab
     *
     * @param isopen
     */
    public void setTabImage(boolean isopen) {
        Message msg = handler.obtainMessage();
        msg.what = MSG_ID_UPDATE_TAB_NOMAL;
        Bundle bundle = new Bundle();
        bundle.putBoolean("isopen", isopen);
        msg.setData(bundle);
        handler.sendMessage(msg);
    }

    /**
     * 初始化fragment
     */
	public void setFragmentIndicator(final int whichIsDefault) {

        if (null == mFragments ) {
            mFragments = new Fragment[4];
            mFragments[0] = getSupportFragmentManager().findFragmentById(R.id.fragment_dial);
            mFragments[1] = getSupportFragmentManager().findFragmentById(R.id.fragment_contacts_list);
            mFragments[2] = getSupportFragmentManager().findFragmentById(R.id.fragment_found);
            mFragments[3] = getSupportFragmentManager().findFragmentById(R.id.fragment_myself);
            
        }    
        boolean isopen = VsUserConfig.getDataBoolean(mContext, VsUserConfig.JKEY_KEYBORD_IS_SHOW, true);
        setView(whichIsDefault, isopen);
        showFragment(whichIsDefault);
        mIndicator.setIndicator(whichIsDefault);
        mIndicator.setOnIndicateListener(new OnIndicateListener() {
            @Override
            public void onIndicate(View v, int which) {           	 
                // 拨号盘开启状态
                boolean isopen = VsUserConfig.getDataBoolean(mContext, VsUserConfig.JKEY_KEYBORD_IS_SHOW, true);
                CustomLog.i("vsdebug", "isopen=" + isopen + "-----上次选中是curIndicator：" + GlobalVariables.curIndicator + "-----本次选中是which：" + which);
                flag = GlobalVariables.curIndicator;
                showFragment(which);
                setView(which, isopen);
                indicator = which;
                GlobalVariables.curIndicator = which;              
                if (which == indicator && which != 0){
                    return;
                }
            }
        });
    }
	
	public void setView(int which,boolean isopen){	
		if(which == 1 || which == 2 || which == 3){
			findViewById(R.id.custom).setVisibility(View.VISIBLE);
			mContactsTitleBarView.setVisibility(View.GONE);
			titleMain.setVisibility(View.GONE);
		}else{
			findViewById(R.id.custom).setVisibility(View.GONE);
			titleMain.setVisibility(View.VISIBLE);
			mContactsTitleBarView.setVisibility(View.VISIBLE);
		}
		if (which == 0) {
            Intent intent = new Intent(VsUserConfig.JKey_CLOSE_USER_LEAD);
            if (GlobalVariables.curIndicator == 0) {
            	 mIndicator.setTabText(isopen);
                // 切换TAB改变清空编辑框数据
                if (isopen) {
                    intent.putExtra("isopen", false);
                    intent.putExtra("indicator", indicator);
                } else {
                    intent.putExtra("isopen", true);
                    intent.putExtra("indicator", indicator);
                }
            } else {
                setTabImageFouse(isopen);
            }  
            mContactsTitleBarView.setTextMms(0,false); //更新信息
            Fragment f=mFragments[0];
//            ((VsDialFragment) f).initTitle();
            mContext.sendBroadcast(intent);                  
        } else if (which == 1) {
//        	mContactsTitleBarView.setTextMms(unRedMessageCount,true); //更新信息
            titleView.setText(getResources().getString(R.string.tab_contacts_list));
            tv_left.setVisibility(View.GONE);
            tv_right.setVisibility(View.VISIBLE);
            inittitlecontact();
//        	inittitlebar();
            setTabImage(isopen);// 设置键盘图标
//			mContactsTitleBarView.getTitleLeft().setEnabled(false);
//			mContactsTitleBarView.getTitleRight().setEnabled(true);
        } else if (which == 2) {              
        	tv_left.setVisibility(View.GONE);
        	tv_right.setVisibility(View.GONE);
        	titleView.setText(getResources().getString(R.string.tab_found));               	
            setTabImage(isopen);// 设置键盘图标
        } else if (which == 3) {
            titleView.setText(getResources().getString(R.string.tab_myself));
            tv_left.setVisibility(View.GONE);
            tv_right.setVisibility(View.GONE);
            setTabImage(isopen);// 设置键盘图标
            if (GlobalVariables.curIndicator != 3) {
                mContext.sendBroadcast(new Intent(VsUserConfig.JKEY_SEARCH_BALANCE));// 发送查询余额广播
            }
        }
		
	}
	
    private void inittitlecontact(){
    	tv_right.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {					
				VsUtil.addContact(mContext, ""); 
			}
		});
	}
    
    private void inittitlebar(){  
    	 titleMain=(LinearLayout) findViewById(R.id.title_main);
    	 mContactsTitleBarView=(TitleBarView) findViewById(R.id.title_tar);
    	 mContactsTitleBarView.setCommonTitle(View.GONE, View.VISIBLE, View.GONE, View.VISIBLE);    	 
    	 mContactsTitleBarView.setTitleLeft(R.string.vs_contacts_all);//联系人按钮
    	 mContactsTitleBarView.setTitleRight(R.string.vs_msm_all);//信息按钮
    	 mContactsTitleBarView.getBtnLeft().setVisibility(View.INVISIBLE);
    	 mContactsTitleBarView.getTitleLeft().setEnabled(false);
	     mContactsTitleBarView.getTitleRight().setEnabled(true);
	     changRight(true);
    	 mContactsTitleBarView.getTitleLeft().setOnClickListener(new View.OnClickListener() {    			
			@Override
			public void onClick(View v) {
			    if (mContactsTitleBarView.getTitleLeft().isEnabled()) {
			    	mContactsTitleBarView.getTitleLeft().setEnabled(false);
			    	mContactsTitleBarView.getTitleRight().setEnabled(true);
			    }
			      changRight(true);
				  setFragmentIndicator(1);// 跳转到联系人
	              setTabImage(true);
	              closeCallAnimation();
			}
		});//左边按钮
    	 
    	 mContactsTitleBarView.getTitleRight().setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
			    if (mContactsTitleBarView.getTitleRight().isEnabled()) {
			    	mContactsTitleBarView.getTitleLeft().setEnabled(true);
			    	mContactsTitleBarView.getTitleRight().setEnabled(false);
			    }
			      changRight(false);
				  setFragmentIndicator(2);// 跳转信息界面				
			}
		});//右边按钮
    	 
    	 
    }
    
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
	public void changRight(boolean isright){
    	if(isright){
    		mContactsTitleBarView.setTxtRightShow();
    		mContactsTitleBarView.getRightBtn().setVisibility(View.GONE);
//    		mContactsTitleBarView.setTxtRight(R.string.vs_dial_clog_add);
    		mContactsTitleBarView.getTxtRight().setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {					
					VsUtil.addContact(mContext, ""); 
				}
			});
    	}else{
    		mContactsTitleBarView.getRightBtn().setImageResource(R.drawable.vs_invite_add_selector);
    		mContactsTitleBarView.getRightBtn().setVisibility(View.VISIBLE);
    		mContactsTitleBarView.setTxtRightHide();
    		mContactsTitleBarView.getRightBtn().setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
//					 showSelectDialog();//单聊群聊					
				}
			});
    	}
    }
    public void showFragment(int which) { 
    	 android.support.v4.app.FragmentTransaction fragmentTransaction= getSupportFragmentManager().beginTransaction();
//    	 fragmentTransaction.setCustomAnimations(R.anim.slide_left_in,R.anim.slide_right_out);
    	 fragmentTransaction.hide(mFragments[0]).hide(mFragments[1]).hide(mFragments[2]).hide(mFragments[3]).show(mFragments[which]).commitAllowingStateLoss();

    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            VsApplication.getInstance().exit();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onResume() {
        NotificationManager notiManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notiManager.cancel(GlobalVariables.NotificationID);
        VsUserConfig.setData(mContext, VsUserConfig.is_show, false);
        
        if (GlobalVariables.curIndicator != indicator) {
            setFragmentIndicator(GlobalVariables.curIndicator);           
            setTabImage(VsUserConfig.getDataBoolean(mContext, VsUserConfig.JKEY_KEYBORD_IS_SHOW, true));// 设置键盘图标
        }
        indicator = GlobalVariables.curIndicator;
        if (indicator != 0) {
            setTabImage(false);
        }
        CustomLog.i(TAG, "VsPhoneCallHistory.CONTACTLIST.size() = " + VsPhoneCallHistory.CONTACTLIST.size());
        super.onResume();
        CustomLog.i(TAG, "MainFragment------onResume(),...");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        CustomLog.i(TAG, "onActivityResult(),.................");
        /**使用SSO授权必须添加如下代码 */
        UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.share");
        UMSsoHandler ssoHandler = mController.getConfig().getSsoHandler(requestCode) ;
        if(ssoHandler != null){
            ssoHandler.authorizeCallBack(requestCode, resultCode, data);
        }
    }

    /*private void registerReceiver() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(AdManager.ACTION_UPDATE_AD);
        registerReceiver(mListenAdReceiver, filter);
    }

    private void unregisterReceiver() {
        unregisterReceiver(mListenAdReceiver);
    }*/

    @Override
    protected void onPause() {
        VsUserConfig.setData(mContext, VsUserConfig.is_show, true);
        
        VsUtil.unregisterCallNumberBC(this);
        
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        if (openOrCloseCall != null) {
            mContext.unregisterReceiver(openOrCloseCall);
            openOrCloseCall = null;
        }
       unregisterReceiver();
        super.onDestroy();
    }

    /**
     * @instruction 消息提醒
     * @author 黄发兴
     * @version 创建时间：2014-12-14 下午03:57:28
     */
    int unRedMessageCount;



    /**
     * @instruction 好友列表被清空时再加载
     * @author Sundy 兴
     * @version 创建时间：2014-12-18 下午01:19:25
     */
    public void getFriends() {
        CustomLog.i(TAG, "进入取本地保存Vs好友");
//        ConnectionIm.getInstance().changeContactsData(mContext, VsUserConfig.getDataString(mContext, VsUserConfig.JKEY_GETVSUSERINFO));
    }


    private void showBalance() {
        String blance = VsUserConfig.getDataString(mContext, VsUserConfig.JKEY_BALANCE_SAVE);
        String vaild_date = VsUserConfig.getDataString(mContext, VsUserConfig.JKEY_VALID_DATE);
        // 判断是否有余额
        if (blance != null && !"".equals(blance) && Float.valueOf(blance) > 0) {// 余额不为零
            int day = VsUtil.compareDate(mContext, vaild_date);
            if (day != 0 && day == 3) {
                VsUtil.showYesNoDialog(mContext, null, "余额还有三天到期", getResources().getString(R.string.vs_start_login_str), getResources().getString(R.string.vs_cannel), null, null);
            }
        } else if (Float.valueOf(blance) == 0) {// 余额为零
            if (!VsUserConfig.getDataBoolean(mContext, VsUserConfig.JKEY_BALANCE_HINT, false)) {
                final CustomDialog2.Builder builder = new CustomDialog2.Builder(mContext);
                final CustomDialog2 dialog = builder.create();
                builder.getDialog_call().setVisibility(View.GONE);
                builder.getDialog_call_line().setVisibility(View.GONE);
                builder.getDialog_inivt().setVisibility(View.GONE);
                builder.getDialog_inivt_line().setVisibility(View.GONE);
                builder.getDialogMessage().setText("余额为0");
                builder.getDialog_chioce().setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        if (VsUserConfig.getDataBoolean(mContext, VsUserConfig.JKEY_BALANCE_HINT, false)) {
                            VsUserConfig.setData(mContext, VsUserConfig.JKEY_BALANCE_HINT, false);
                        } else {
                            VsUserConfig.setData(mContext, VsUserConfig.JKEY_BALANCE_HINT, true);
                        }
                    }
                });
                dialog.show();
            }
        }
    }

}



